package com.kethink;

import java.util.*;

/**
 * @Description create by xuxiaojun
 * date: 2018/12/25
 * Time: 21:30
 */
public class LeeCode_811 {

    public static void main(String[] args){

       String[] S = {"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"};
//        String[] S = {"9001 discuss.leetcode.com"};
        System.out.println(subdomainVisits(S));
    }

    public static List<String> subdomainVisits(String[] cpdomains) {
        HashMap<String,Integer> map = new HashMap();
        for(String str : cpdomains){
            String[] s = str.split(" ");
            //获取次数
            int s0 = Integer.valueOf(s[0]);
            //获取域名
            String s1 = s[1];
            if(!map.containsKey(s1)){
                map.put(s1,s0);
            }else{
                map.put(s1,s0+map.get(s1));
            }
            while (s1.contains(".")){
                int i = s1.indexOf(".")+1;
                int j = s1.length();
                s1 = s1.substring(i,j);
                //判断是否已经不需要分割，不需要就保存返回
                if(!s1.contains(".")){
                    if(!map.containsKey(s1)){
                        map.put(s1,s0);
                        break;
                    }else{
                        map.put(s1,s0+map.get(s1));
                        break;
                    }
                }
                //还需要继续分割
                if(!map.containsKey(s1)){
                    map.put(s1,s0);
                }else{
                    map.put(s1,s0+map.get(s1));
                }
            }
        }
        String s = "";
        List list = new ArrayList();
        Set<String> strings = map.keySet();
        for(String st : strings){
            s = map.get(st)+" "+ st;
            list.add(s);
        }
        return list;
    }
}
